package com.zhs.gccs.activity.gccs;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.zhs.gccs.R;
import com.zhs.gccs.adapter.ApplyGccsTaskAdapter;
import com.zhs.gccs.base.BaseActivity;
import com.zhs.gccs.http.Api;
import com.zhs.gccs.http.ProgressObserver;
import com.zhs.gccs.utils.OnNoDoubleClickListener;
import com.zhs.gccs.vo.ApplyGccsTask;
import com.zhs.gccs.vo.TaskListBean;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GccsUserTaskListActivity extends BaseActivity {
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.ll_root)
    LinearLayout llRoot;
    @BindView(R.id.iv_back)
    ImageView iv_back;

    private List<ApplyGccsTask> projects;
    private ApplyGccsTaskAdapter projectQueryAdapter;

    private int pageNum = 0;
    private int pageSize = 10;

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        pageNum = 0;
        getProjectTaskData();
    }

    public static void start(Context context) {
        context.startActivity(new Intent(context, GccsUserTaskListActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gccs_user_task_list);
        ImmersionBar.with(this).transparentBar().statusBarDarkFont(false).init();
        ButterKnife.bind(this);
        llRoot.setPadding(0, ImmersionBar.getStatusBarHeight(this), 0, 0);

        projects = new ArrayList<>();
        projectQueryAdapter = new ApplyGccsTaskAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(projectQueryAdapter);

        projectQueryAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                if (view.getId() == R.id.btn_task){
                    ApplyGccsTask bean = projectQueryAdapter.getData().get(i);
                    GccsTaskMenuActivity.start(GccsUserTaskListActivity.this, bean);
                }
            }
        });

        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                if (parent.getChildAdapterPosition(view) == 0) {
                    outRect.top = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8F, Resources.getSystem().getDisplayMetrics());
                }
                outRect.bottom = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8F, Resources.getSystem().getDisplayMetrics());
                outRect.left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10F, Resources.getSystem().getDisplayMetrics());
                outRect.right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10F, Resources.getSystem().getDisplayMetrics());
            }
        });
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
//                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                pageNum = 0;
                getProjectTaskData();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
//                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                getProjectTaskData();
            }
        });

        initView();
        getProjectTaskData();
    }

    private void initView() {
        iv_back.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
//                DialogUtils.showPromote2(context, "是否退出登录", null, new DialogUtils.OnConfirmListener() {
//                    @Override
//                    public void onConfirmed() {
//                        LoginActivity.start(GccsUserTaskListActivity.this);
//                    }
//                });
                finish();
            }
        });
    }

    private void getProjectTaskData() {
        showDialog();
        toSubscribeSingleOb(Api.getDefault().gccsExecList(pageSize, pageNum),
                new ProgressObserver(this, false) {
                    @Override
                    protected void _onNext(String result) {
                        Type type = TypeToken.getParameterized(TaskListBean.class, ApplyGccsTask.class).getType();
                        TaskListBean bean = new Gson().fromJson(result, type);//GsonImpl.get().toObject(result, SmTaskBean.class);
                        if (pageNum == 0) {
                            projects.clear();
                        }
                        if (bean != null && bean.getCode() == 200 && bean.getRows() != null && bean.getRows().size() > 0) {
                            projects.addAll(bean.getRows());
                            finishLoadMore();
                            int total = bean.getTotal();
                            if (total > ((pageNum + 1) * pageSize)) {
                                ++pageNum;
                                refreshLayout.setEnableLoadMore(true);
                            } else {
                                refreshLayout.setEnableLoadMore(false);
                            }
                        } else {
                            finishLoadMore();
                            refreshLayout.setEnableLoadMore(false);
                        }
                        projectQueryAdapter.setNewData(projects);
                    }

                    @Override
                    protected void _onError(String result) {
                        finishLoadMore();
                        refreshLayout.setEnableLoadMore(false);
                    }
                });
    }

    private void finishLoadMore() {
        dismissDialog();
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
    }

}
